package com.feng.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.feng.cloud.common.annotation.ControllerEndpoint;
import com.feng.cloud.common.response.RestResponse;
import com.feng.cloud.system.dto.MenuTree;
import com.feng.cloud.system.dto.VueRouter;
import com.feng.cloud.system.entity.Menu;
import com.feng.cloud.system.service.MenuService;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuController
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/21 22:09
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("menu")
@RequiredArgsConstructor
public class MenuController implements IMenuController {

    private final MenuService menuService;

    /**
     * 查询菜单树
     * @param menu menu
     * @return
     */
    @GetMapping("tree")
    @PreAuthorize("hasAuthority('menu:view')")
    public RestResponse<List<MenuTree>> selectMenuTree(Menu menu) {
        return new RestResponse<List<MenuTree>>().data(menuService.selectMenuTree(menu));
    }

    /**
     * 根据id删除菜单
     * @param menuIds menuIds
     * @return RestResponse<Void>
     */
    @Override
    @PreAuthorize("hasAuthority('menu:delete')")
    @ControllerEndpoint(operation = "删除菜单/按钮", exceptionMessage = "删除菜单/按钮失败")
    public RestResponse<Void> deleteMenuByIds(@NotBlank(message = "{required}") String menuIds) {
        menuService.deleteMenuByIds(Arrays.asList(menuIds.split(StringPool.COMMA)));
        return new RestResponse<>();
    }

    /**
     * 修改菜单
     * @param menu menu
     * @return RestResponse<Menu>
     */
    @Override
    @PreAuthorize("hasAuthority('menu:update')")
    @ControllerEndpoint(operation = "修改菜单/按钮", exceptionMessage = "修改菜单/按钮失败")
    public RestResponse<Menu> updateMenu(@Valid Menu menu) {
        menuService.updateMenu(menu);
        return new RestResponse<Menu>().data(menu);
    }

    /**
     * 增加菜单
     * @param menu menu
     * @return RestResponse<Menu>
     */
    @Override
    @PreAuthorize("hasAuthority('menu:add')")
    @ControllerEndpoint(operation = "新增菜单/按钮", exceptionMessage = "新增菜单/按钮失败")
    public RestResponse<Menu> addMenu(@Valid Menu menu) {
        menuService.addMenu(menu);
        return new RestResponse<Menu>().data(menu);
    }

    @Override
    public RestResponse<Map<String, Object>> getUserRouters(@NotBlank(message = "{required}") String username) {
        Map<String, Object> result = new HashMap<>();
        List<VueRouter<Menu>> userRouters = this.menuService.getUserRouters(username);
        result.put("routes", userRouters);
        List<String> userPermissions = this.menuService.findUserPermissions(username);
        result.put("permissions", userPermissions.toArray(new String[userPermissions.size()]));
        return new RestResponse<Map<String, Object>>().data(result);
    }

    @PostMapping("excel")
    @PreAuthorize("hasAuthority('menu:export')")
    @ControllerEndpoint(operation = "导出菜单数据", exceptionMessage = "导出Excel失败")
    public void export(Menu menu, HttpServletResponse response) {
        List<Menu> menus = this.menuService.selectList(menu);
        ExcelKit.$Export(Menu.class, response).downXlsx(menus, false);
    }
}
